package com.github.east196.terminator.xtend.bot;

import com.github.east196.terminator.xtend.model.Field;
import com.google.common.base.Objects;
import com.google.common.io.Files;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Generated;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
@Generated("org.eclipse.xtend.core.compiler.XtendGenerator")
public class FXmlBot {
  private static int i = 1;
  
  public static void main(final String[] args) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("D:\\workspace\\east196\\java\\terminator\\terminator-fx\\src\\main\\resources\\fxml\\initializers.fxml");
      String path = _builder.toString();
      File _file = new File(path);
      File _file_1 = new File((path + ".bak"));
      Files.copy(_file, _file_1);
      FXmlBot.handleXml(path);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static CharSequence handleXml(final String path) {
    try {
      CharSequence _xblockexpression = null;
      {
        SAXReader reader = new SAXReader();
        final File file = new File(path);
        final String fileName = file.getName().split("\\.")[0];
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List<String> fxids = FXmlBot.collectFxIds(root);
        List<String> imports = FXmlBot.collectImports(document);
        CharSequence bean = FXmlBot.geneBean(fxids, imports, fileName);
        InputOutput.<CharSequence>print(bean);
        CharSequence ctl = FXmlBot.geneController(fxids, imports, fileName);
        _xblockexpression = InputOutput.<CharSequence>print(ctl);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static List<String> collectFxIds(final Element root) {
    List<String> _xblockexpression = null;
    {
      List<String> fxids = CollectionLiterals.<String>newArrayList();
      String _name = root.getName();
      String _plus = ("=====开始遍历=====" + _name);
      System.out.println(_plus);
      List<Attribute> rootAttrs = root.attributes();
      for (final Attribute attr : rootAttrs) {
        {
          StringConcatenation _builder = new StringConcatenation();
          _builder.append("属性名：");
          String _name_1 = attr.getName();
          _builder.append(_name_1);
          _builder.append(" ");
          String _qualifiedName = attr.getQualifiedName();
          _builder.append(_qualifiedName);
          _builder.append("--属性值：");
          String _value = attr.getValue();
          _builder.append(_value);
          System.out.println(_builder);
          String _qualifiedName_1 = attr.getQualifiedName();
          boolean _equals = Objects.equal(_qualifiedName_1, "fx:id");
          if (_equals) {
            String _value_1 = attr.getValue();
            String _plus_1 = (_value_1 + "__");
            String _name_2 = root.getName();
            String _plus_2 = (_plus_1 + _name_2);
            fxids.add(_plus_2);
          }
        }
      }
      Iterator<Element> itt = root.elementIterator();
      while (itt.hasNext()) {
        {
          Element _next = itt.next();
          Element bookChild = ((Element) _next);
          StringConcatenation _builder = new StringConcatenation();
          _builder.append("节点名：");
          String _name_1 = bookChild.getName();
          _builder.append(_name_1);
          System.out.println(_builder);
          fxids.addAll(FXmlBot.collectFxIds(bookChild));
        }
      }
      String _name_1 = root.getName();
      String _plus_1 = ("=====结束遍历=====" + _name_1);
      System.out.println(_plus_1);
      _xblockexpression = fxids;
    }
    return _xblockexpression;
  }
  
  public static List<String> collectImports(final Document document) {
    List<String> _xblockexpression = null;
    {
      List<String> imports = CollectionLiterals.<String>newArrayList();
      System.out.println("=====开始遍历=====");
      final Iterator<Node> nodeIterator = document.nodeIterator();
      while (nodeIterator.hasNext()) {
        {
          Node _next = nodeIterator.next();
          Node node = ((Node) _next);
          String _name = node.getName();
          boolean _equals = Objects.equal(_name, "import");
          if (_equals) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("节点名：");
            String _name_1 = node.getName();
            _builder.append(_name_1);
            _builder.append(" ");
            String _stringValue = node.getStringValue();
            _builder.append(_stringValue);
            System.out.println(_builder);
            imports.add(node.getStringValue());
          }
        }
      }
      System.out.println("=====结束遍历=====");
      _xblockexpression = imports;
    }
    return _xblockexpression;
  }
  
  public static CharSequence geneController(final List<String> fxids, final List<String> imports, final String fileName) {
    CharSequence _xblockexpression = null;
    {
      String klassType = StringExtensions.toFirstUpper(fileName);
      final Function1<String, Field> _function = (String fxid) -> {
        InputOutput.<String>print(fxid);
        Field field = new Field();
        field.setName(fxid.split("\\__")[0]);
        field.setType(fxid.split("\\__")[1]);
        return field;
      };
      List<Field> fields = ListExtensions.<String, Field>map(fxids, _function);
      InputOutput.<List<Field>>print(fields);
      InputOutput.<String>print("/n");
      StringConcatenation _builder = new StringConcatenation();
      _builder.newLine();
      _builder.newLine();
      {
        for(final Field f : fields) {
          _builder.newLine();
          _builder.append("@FXML");
          _builder.newLine();
          _builder.append("private ");
          String _type = f.getType();
          _builder.append(_type);
          _builder.append(" ");
          String _javaName = f.javaName();
          _builder.append(_javaName);
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("\t\t");
      _builder.newLine();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
  
  public static CharSequence geneBean(final List<String> fxids, final List<String> imports, final String fileName) {
    CharSequence _xblockexpression = null;
    {
      String klassType = StringExtensions.toFirstUpper(fileName);
      final Function1<String, Field> _function = (String fxid) -> {
        InputOutput.<String>print(fxid);
        Field field = new Field();
        field.setName(fxid.split("\\__")[0]);
        field.setType(fxid.split("\\__")[1]);
        return field;
      };
      List<Field> fields = ListExtensions.<String, Field>map(fxids, _function);
      InputOutput.<List<Field>>print(fields);
      InputOutput.<String>print("/n");
      StringConcatenation _builder = new StringConcatenation();
      _builder.newLine();
      _builder.append("import org.apache.commons.lang3.builder.HashCodeBuilder;");
      _builder.newLine();
      _builder.append("import org.apache.commons.lang3.builder.EqualsBuilder;");
      _builder.newLine();
      _builder.append("import org.apache.commons.lang3.builder.ToStringBuilder;");
      _builder.newLine();
      _builder.append("import org.apache.commons.lang3.builder.ToStringStyle;");
      _builder.newLine();
      _builder.newLine();
      _builder.append("import cn.edu.scau.biubiusuisui.annotation.FXEntity;");
      _builder.newLine();
      _builder.append("import cn.edu.scau.biubiusuisui.annotation.FXField;");
      _builder.newLine();
      _builder.newLine();
      {
        for(final String i : imports) {
          _builder.append("import ");
          _builder.append(i);
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.newLine();
      _builder.append("@FXEntity");
      _builder.newLine();
      _builder.append("public class ");
      _builder.append(klassType);
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      _builder.append("\t");
      _builder.newLine();
      {
        for(final Field f : fields) {
          _builder.append("\t");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("@FXField");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("private ");
          String _type = f.getType();
          _builder.append(_type, "\t");
          _builder.append(" ");
          String _javaName = f.javaName();
          _builder.append(_javaName, "\t");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("\t");
      _builder.newLine();
      {
        for(final Field f_1 : fields) {
          _builder.append("\t");
          _builder.append("public ");
          String _type_1 = f_1.getType();
          _builder.append(_type_1, "\t");
          _builder.append(" get");
          String _firstUpper = StringExtensions.toFirstUpper(f_1.javaName());
          _builder.append(_firstUpper, "\t");
          _builder.append("() {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("\t");
          _builder.append("return ");
          String _javaName_1 = f_1.javaName();
          _builder.append(_javaName_1, "\t\t");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("}");
          _builder.newLine();
          _builder.newLine();
          _builder.append("\t");
          _builder.append("public void set");
          String _firstUpper_1 = StringExtensions.toFirstUpper(f_1.javaName());
          _builder.append(_firstUpper_1, "\t");
          _builder.append("(");
          String _type_2 = f_1.getType();
          _builder.append(_type_2, "\t");
          _builder.append(" ");
          String _javaName_2 = f_1.javaName();
          _builder.append(_javaName_2, "\t");
          _builder.append(") {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("\t");
          _builder.append("this.");
          String _javaName_3 = f_1.javaName();
          _builder.append(_javaName_3, "\t\t");
          _builder.append(" = ");
          String _javaName_4 = f_1.javaName();
          _builder.append(_javaName_4, "\t\t");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("}");
          _builder.newLine();
          _builder.append("\t");
          _builder.newLine();
        }
      }
      _builder.append("\t");
      _builder.append("@Override ");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("public int hashCode() {");
      _builder.newLine();
      _builder.append("\t\t");
      _builder.append("return HashCodeBuilder.reflectionHashCode(this);");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      _builder.append("\t");
      _builder.append("@Override ");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("public boolean equals(Object other) {");
      _builder.newLine();
      _builder.append("\t\t");
      _builder.append("return EqualsBuilder.reflectionEquals(this, other);");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      _builder.append("\t");
      _builder.append("@Override ");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("public String toString() {");
      _builder.newLine();
      _builder.append("\t\t");
      _builder.append("return ToStringBuilder.reflectionToString(this,ToStringStyle.DEFAULT_STYLE);");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("}");
      _builder.newLine();
      _builder.append("\t");
      _builder.newLine();
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
}
